<?php
session_start();
define('IN_ECS', true);
require('../includes/init.php');
include_once('../includes/lib_transaction.php');
include_once('../includes/lib_passport.php');

include_once( 'config.php' );
include_once( 'saetv2.ex.class.php' );

$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );

if (isset($_REQUEST['code'])) {
	$keys = array();
	$keys['code'] = $_REQUEST['code'];
	$keys['redirect_uri'] = WB_CALLBACK_URL;
	try {
		$token = $o->getAccessToken( 'code', $keys ) ;
	} catch (OAuthException $e) {
	}
}


if ($token) {
	$_SESSION['token'] = $token;
	setcookie( 'weibojs_'.$o->client_id, http_build_query($token) );
	
	$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $_SESSION['token']['access_token'] );
$ms  = $c->home_timeline(); // done
$uid_get = $c->get_uid();
$uid = $uid_get['uid'];
$user_message = $c->show_user_by_id( $uid);//根据ID获取用户等基本信息



function check_user($username){
$sql = "SELECT user_id, password, salt " .
                   " FROM " . $GLOBALS['ecs']->table("users").
                   " WHERE user_name='$username'";
            $row = $GLOBALS['db']->getRow($sql);
            if (!empty($row)){
    return true;
   }else{
    return false;
    }
}
if($user_message['screen_name']!==""){ //获取微薄昵称
  $username=$user_message['screen_name'];
  $password=time();//随便弄个密码 反正没有用
  $email='123456@163.com';//随便默认个邮箱
  $back_act ="/user.php";
        if (check_user($username)!==false){
   $GLOBALS['user']->set_session($username);
         $GLOBALS['user']->set_cookie($username);
            header("Location: /user.php\n");  //验证成功,跳转页面
   exit;
        }else{
   $reg_date = time();
   $password =md5($password);
   $ip=real_ip();
   $GLOBALS['db']->query('INSERT INTO ' . $GLOBALS['ecs']->table("users") . "(`email`, `user_name`, `password`, `reg_time`, `last_login`, `last_ip`) VALUES ('$email', '$username', '$password', '$reg_date', '$reg_date', '$ip')");
   $GLOBALS['user']->set_session($username);
         $GLOBALS['user']->set_cookie($username);
   header("Location: /user.php\n");
   exit;
   }
}else{
  echo 'fail';
  exit;
  }
?>
 
<?php
} else {
?>
授权失败。
<?php
}
?>